﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DATN.Business;
using DATN.Common;

namespace DATN.Share
{
    public partial class UcDangNhap : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie cookie = Request.Cookies["cookieUser"];
            if (cookie != null)
            {
                AnTextbox();
                lblStatus.Text = cookie["cookieUser"] + ", đã đăng nhập.";
            }
            else
            {
                HienTextbox();
            }
        }

        protected void BtLogin_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUserName.Text) || string.IsNullOrEmpty(txtPassword.Text))
            {
                lblStatus.Text = "Hãy nhập tài khoản.";
                return;
            }
            UserBusiness userManager = new UserBusiness();

            //Get Role By Account, Password
            HttpCookie cookieCheck = Request.Cookies["cookieUser"];
            HttpCookie cookieUser = new HttpCookie("cookieUser");

            int Role = userManager.GetRole(txtUserName.Text, txtPassword.Text);
            if (Role == -1)
            {
                Role = userManager.GetRole(txtUserName.Text, Utility.GetMD5(txtPassword.Text));

                if (Role > 0)
                {

                    //ẩn textbox
                    AnTextbox();

                    //Set Cookie User Info
                    cookieUser["cookieUser"] = txtUserName.Text;
                    cookieUser["cookiePassword"] = Utility.GetMD5(txtPassword.Text);
                    cookieUser.Expires = DateTime.Now.AddDays(30);
                    Response.Cookies.Add(cookieUser);
                }
            }
            else
            {
                //Set Cookie User Info
                cookieUser["cookieUser"] = txtUserName.Text;
                cookieUser["cookiePassword"] = txtPassword.Text;
                cookieUser.Expires = DateTime.Now.AddDays(30);
                Response.Cookies.Add(cookieUser);
            }
            //}
            if (Role == -1)
            {
                lblStatus.Text = "Tài khoản sai!";
                return;
            }

            if (CheckBoxRemember.Checked == false)
            {
                // Cookie status Checkbox Remember Account, Password
                HttpCookie cookieState = new HttpCookie("cookieState");
                cookieState.Expires = DateTime.Now;
                Response.Cookies.Add(cookieState);
                cookieState["cookieState"] = "0";
            }
            else
            {
                // Cookie status Checkbox Remember Account, Password
                HttpCookie cookieState = new HttpCookie("cookieState");
                cookieState.Expires = DateTime.Now.AddDays(10);
                Response.Cookies.Add(cookieState);
                cookieState["cookieState"] = "1";
            }
            Session["Role"] = Role;
        }

        private void AnTextbox()
        {
            lblStatus.Text = txtUserName.Text + ", đã đăng nhập.";
            //linkThoat.Visible = true;
            btThoat.Visible = true;
            dk.Visible = false;
            qmk.Visible = false;
            changePass.Visible = true;
            txtUserName.Visible = false;
            txtPassword.Visible = false;
            BtLogin.Visible = false;
            lbTen.Visible = false;
            lbPass.Visible = false;
        }

        private void HienTextbox()
        {
            lblStatus.Text = "";
            //linkThoat.Visible = false;
            dk.Visible = true;
            qmk.Visible = true;
            changePass.Visible = false;
            btThoat.Visible = false;
            txtUserName.Visible = true;
            txtPassword.Visible = true;
            BtLogin.Visible = true;
            lbTen.Visible = true;
            lbPass.Visible = true;
        }

        protected void btThoat_Click(object sender, EventArgs e)
        {

            HttpCookie cookieStateCheck = Request.Cookies["cookieState"];

            //if (cookieStateCheck != null)
            //{
            //    string str = cookieStateCheck["cookieState"];
            //    if (str == "0" || str == null)
            //    {
            HttpCookie cookieUser = new HttpCookie("cookieUser");
            cookieUser["cookieUser"] = "";
            cookieUser.Expires = DateTime.Now;
            Response.Cookies.Add(cookieUser);
            //    }
            //}
            //Hiện textbox
            HienTextbox();

            Session["Role"] = null;
            Session.Abandon();
        }
    }
}